var t1;
//当前页面显示个数
var size = 20;
$(document).ready(function(){
    // setInterval(ajaxGetData,60000);
    ajaxGetData();
    //设置每一分钟循环一次
    window.setInterval(refreshInfo, 60000);
    function refreshInfo() {
        //把3秒的定时器关闭
        window.clearInterval(t1);
        console.log("----------------"+CurentTime()); //1495302061441
        ajaxGetData();
    }
});

function ajaxGetData(){
    $.ajax({
        url : "http://10.0.1.101/climb/erpOrder/getDisplay",
        type : "GET",
        dataType : "jsonp", // 返回的数据类型，设置为JSONP方式
        jsonpCallback: 'success', //设置回调函数名
        success : function(res) {
            var tmArr = res;
            //清空div标签中的数据
            $('#message').empty();
            createTable(tmArr);
        }
    });
}

function createTable(tmArr){
    var str = "";
    str +="<table border='1' rules='all' borderColor='#F00' width='1760px' height='960px' bgcolor='#000000' style='color: #F00;text-align: center;'>" +
        "<thead><tr><th>系统编号</th><th>车辆信息</th><th>客户名称</th><th>装货产品</th>" +
        "<th>装卸平台</th><th>应发数量（kg）</th><th>已发数量（kg）</th></tr></thead><tbody>";
    for (var i = 0;i < size;i++){
        str +="<tr><td>"+tmArr[i].sysCode+"</td><td>"+tmArr[i].vehicleInfo+"</td><td>"+tmArr[i].customerName+"</td><td>" +
            +tmArr[i].productName+"</td><td>"+tmArr[i].loadUnloadPlatform+"</td><td>"+tmArr[i].quality+"</td><td>"+tmArr[i].outQuality+"</td></tr>";
    }
    //遍历完成之后
    str +="</tbody></table>";
    //将表格添加到body中
    $('#message').append(str);
    var i = size;
    t1=window.setInterval(refreshCount, 3000);
    function refreshCount() {
        console.log("-----"+i+"    "+CurentTime());
        deleteFirstRow();
        insertToLastRow(tmArr[i]);
        i++;
        if (i == tmArr.length){
            i = 0;
        }
    }
}

//插入最后一行
function insertToLastRow(tm){
    var str = "<tr><td>"+tmArr[i].sysCode+"</td><td>"+tmArr[i].vehicleInfo+"</td><td>"+tmArr[i].customerName+"</td><td>" +
        +tmArr[i].productName+"</td><td>"+tmArr[i].loadUnloadPlatform+"</td><td>"+tmArr[i].quality+"</td><td>"+tmArr[i].outQuality+"</td></tr>";
    $("#mess").append(str);
}

//删除第一行
function deleteFirstRow(){
    $("table tr:eq(1)").remove();
}
function CurentTime() {
    var now = new Date();

    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var ss = now.getSeconds();           //秒

    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";

    if (ss < 10) clock += '0';
    clock += ss;
    return(clock);
}